package com.yixin.hospital.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.Dept;
import com.yixin.hospital.entity.domain.WorkAddress;
import com.yixin.hospital.entity.dto.req.workaddress.WorkAddressAddReqDTO;
import com.yixin.hospital.entity.dto.req.workaddress.WorkAddressEditReqDTO;
import com.yixin.hospital.entity.vo.WorkAddressVO;
import com.yixin.hospital.service.DeptService;
import com.yixin.hospital.service.WorkAddressService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/work-address")
@Api(value = "工作地点管理", tags = "工作地点管理")
public class WorkAddressController {

    @Resource
    WorkAddressService workAddressService;
    @Resource
    DeptService deptService;

    @GetMapping("/list")
    @ApiOperation(value = "工作地点管理列表")
    public ApiResult list() {
        List<WorkAddress> list = workAddressService.list();
        List<WorkAddressVO> workAddressVOS = BeanUtil.copyToList(list, WorkAddressVO.class);
        for (WorkAddressVO workAddressVO : workAddressVOS) {
            Dept dept = deptService.getById(workAddressVO.getDeptId());
            workAddressVO.setDeptName(dept.getName());
        }
        return ApiResult.success(workAddressVOS);
    }

    @GetMapping("/dept-work-address")
    @ApiOperation(value = "根据部门查询工作地点")
    @ApiImplicitParams({
            @ApiImplicitParam(name="deptId",value="部门id", paramType = "query")
    })
    public ApiResult deptWorkAddress(@RequestParam Long deptId) {
        LambdaQueryWrapper<WorkAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkAddress::getDeptId, deptId);
        List<WorkAddress> list = workAddressService.list(queryWrapper);
        return ApiResult.success(list);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加工作地点管理")
    public ApiResult add(@Valid @RequestBody WorkAddressAddReqDTO workAddressAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        WorkAddress workAddress = new WorkAddress();
        workAddress.setName(workAddressAddReqDTO.getName());
        workAddress.setDeptId(workAddressAddReqDTO.getDeptId());
        workAddress.setCreatedBy(userName);
        workAddress.setUpdatedBy(userName);
        workAddressService.save(workAddress);
        return ApiResult.success();
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑工作地点管理")
    public ApiResult edit(@Valid @RequestBody WorkAddressEditReqDTO workAddressEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        WorkAddress workAddress = new WorkAddress();
        workAddress.setId(workAddressEditReqDTO.getId());
        workAddress.setName(workAddressEditReqDTO.getName());
        workAddress.setDeptId(workAddressEditReqDTO.getDeptId());
        workAddress.setUpdatedBy(userName);

        workAddressService.updateById(workAddress);
        return ApiResult.success();
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除工作地点管理")
    public ApiResult remove(@PathVariable int id) {
        workAddressService.removeById(id);
        return ApiResult.success();
    }
}
